/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.dao.impl;

import com.legendshop.dao.impl.GenericDaoImpl;
import com.legendshop.dao.support.*;
import com.legendshop.dao.sql.ConfigCode;
import com.legendshop.oa.dao.DailyLogDao;
import com.legendshop.oa.dto.DailyLogEntity;
import com.legendshop.oa.dto.DailyLogQueryDto;
import com.legendshop.oa.model.DailyLog;
import com.legendshop.oa.model.Project;
import com.legendshop.util.DateUtil;

import java.util.Date;
import java.util.List;

/**
 * The Class DailyLogDaoImpl. 日志表Dao实现类
 */

public class DailyLogDaoImpl extends GenericDaoImpl<DailyLog, Long> implements DailyLogDao {

	/**
	 * 根据商城获取日志表列表
	 */
	public List<DailyLog> getDailyLog(String userName) {
		return this.queryByProperties(new EntityCriterion().eq("userName", userName));
	}

	/**
	 * 根据Id获取日志表
	 */
	public DailyLog getDailyLog(Long id) {
		return getById(id);
	}

	/**
	 * 删除日志表
	 */
	public int deleteDailyLog(DailyLog dailyLog) {
		return delete(dailyLog);
	}

	/**
	 * 保存日志表
	 */
	public Long saveDailyLog(DailyLog dailyLog) {
		return save(dailyLog);
	}

	/**
	 * 更新日志表
	 */
	public int updateDailyLog(DailyLog dailyLog) {
		return update(dailyLog);
	}

	/**
	 * 查询日志表列表
	 */
	public PageSupport getDailyLog(CriteriaQuery cq) {
		return queryPage(cq);
	}

	@Override
	public PageSupport<DailyLog> getDailyLog(DailyLogQueryDto dailyLogQueryDto) {
		SimpleSqlQuery query = new SimpleSqlQuery(Project.class, 20, dailyLogQueryDto.getCurPageNO());
		QueryMap map = new QueryMap();
		map.put("projectId", dailyLogQueryDto.getProjectId());
		map.put("moduleId", dailyLogQueryDto.getModuleId());
		map.put("userName", dailyLogQueryDto.getUserName());
		map.put("startTime", dailyLogQueryDto.getStartDate());
		map.put("endTime", dailyLogQueryDto.getEndDate());
		map.put("status", dailyLogQueryDto.getStatus());

		String querySql = ConfigCode.getInstance().getCode("oa.getDailyLog", map);
		String sqlCount = ConfigCode.getInstance().getCode("oa.getDailyLogCount", map);

		query.setParam(map.toArray());
		query.setQueryString(querySql);
		query.setAllCountString(sqlCount);

		return querySimplePage(query);
	}

	@Override
	public List<DailyLogEntity> queryDailyLog(Date startDate, Date endDate, String userName) {
		return query("SELECT id, DATE AS START, DATE AS END, CONCAT(consumed,'h,', WORK) AS title FROM ls_taskestimate WHERE DATE >= ? AND DATE <= ? AND user_name = ?", DailyLogEntity.class, startDate, endDate, userName);
	}

	@Override
	public boolean moveTaskEstimate(Long id, String userName, String date) {
		Date d = DateUtil.StringToDate(date, "yyyy-MM-dd");
		return update("UPDATE ls_taskestimate SET DATE = ? WHERE id = ? AND user_name = ?", d, id, userName) > 0;
	}

}
